<?php

namespace app\modules\Income\models;

use Yii;
use app\modules\Product\models\Product;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "income_claim".
 *
 * @property int $id
 * @property int $product_id 团队ID
 * @property int $income_id 收款ID
 * @property int $currency 货币(美元)
 * @property string $price 认领金额
 * @property int $status 1正常  2 禁用 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class IncomeClaim extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'income_claim';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'income_id', 'currency', 'status', 'user_id','confirm_id'], 'integer'],
            [['price'], 'number','min'=>0],
            [['product_id','price','income_id'],'required'],
            [['created_at', 'updated_at','pro_price','rate'], 'safe'],
            [['code'], 'string', 'max' => 128],
            [['remark'], 'string', 'max' => 1024],
            ['price','validatePrice'],
        ];
    }

    //限制金额大小
    public function validatePrice(){
        $income = Income::findOne($this->income_id);
        if($income){
            if($this->price > $income->notPrice){
                 $this->addError('price', '认领金额不能大于剩余认领金额');
            }
        }
    }

    //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;

            $t = 'ICM';
            $d = date("Ymd");
            $count = IncomeClaim::find()->where(['>=', 'created_at', date("Y-m-d 00:00:00")])->count();

            $num = sprintf("%04d", $count+1);

            $this->code = sprintf("%s%s%s", $t, $d, $num);
        }
        return parent::beforeSave($insert);

    }//}}}

     //  获取团队信息
    public function getProduct(){
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
    }

     //  获取收入信息
    public function getIncome(){
        return $this->hasOne(Income::className(), ['id' => 'income_id']);
    }


    //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'code' => Yii::t('app.income', 'Claim Code'),
            'product_id' => Yii::t('app.income', 'Product ID'),
            'income_id' => Yii::t('app.income', 'Income ID'),
            'currency' => Yii::t('app', 'Currency'),
            'price' => Yii::t('app.income', 'Claim Price'),
            'pro_price' => Yii::t('app.income', 'Pro Price'),
            'rate' => Yii::t('app.income', 'Rate'),
            'remark' => Yii::t('app.supplier', 'Remark'),
            'status' => Yii::t('app', 'Status'),
            'confirm_id' => Yii::t('app', 'Confirm ID'),
            'confirm_at' => Yii::t('app', 'Confirm At'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
